<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<title>Source token and database object</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta name="generator" content="Doc-O-Matic" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <link rel="STYLESHEET" href="default.css" type="text/css" />

<script type="text/javascript" src="scripts.js"></script>
</head>
<body class="Element700" onload="onBodyLoadEx('frames.html', 'topic', 'Source_token_and_database_object.html');" onmousedown="onBodyMouseDown();">

<!-- Begin Popups -->

<!-- End Popups -->

<!-- Begin Page Header -->
<div class="Element710" id="areafixed">
<div class="Element94">
<em>You are here:</em> Using General SQL Parser &gt; <a href="Source_token_and_database_object.html" target="topic">Source token and database object</a></div>
<div class="Element92">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="50%">
<div class="Element2">
<a href="contents.html" target="tocidx"><img src="btn_globals_contents_midblue.gif" border="0" alt="Contents" title="Contents" onmouseover="switchImage(this, 'btn_globals_contents_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_globals_contents_midblue.gif');"></a><a href="idx.html" target="tocidx"><img src="btn_globals_index_midblue.gif" border="0" alt="Index" title="Index" onmouseover="switchImage(this, 'btn_globals_index_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_globals_index_midblue.gif');"></a><a href="index.html" target="topic"><img src="btn_globals_home_midblue.gif" border="0" alt="Home" title="Home" onmouseover="switchImage(this, 'btn_globals_home_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_globals_home_midblue.gif');"></a></div>
</td><td width="50%">
<div class="Element90">
<a href="Set_operation_in_select_statement.html" target="topic"><img src="btn_prev_midblue.gif" border="0" alt="Previous" title="Previous" onmouseover="switchImage(this, 'btn_prev_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_prev_midblue.gif');"></a><img src="btn_up_d.gif" border="0" alt="Up" title="Up"><a href="How_to_refactor_sql.html" target="topic"><img src="btn_next_midblue.gif" border="0" alt="Next" title="Next" onmouseover="switchImage(this, 'btn_next_midblue_hover.gif');" onmouseout="switchImage(this, 'btn_next_midblue.gif');"></a></div>
</td></tr></table><div class="Element5">
Source token and database object</div>
</div>
</div>

<!-- End Page Header -->

<!-- Begin Client Area -->
<div class="Element720" id="areascroll">
<div class="Element721">

<!-- Begin Page Content -->
<div class="Element58">
<a name="Description"></a><div class="Element11">
<div class="Element10">
<p class="Element10">
<strong>Relationship between source token and database object</strong>&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
After parsing sql statement, database object information was stored in source token, you can check property DBObjType of <a href="TSourceToken.html" target="topic">TSourceToken</a> which is type of <a href="TDBObjType.html" target="topic">TDBObjType</a> to find out which database object is that source token represent for if any.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
Since there are lots of types of database object, let's begin with table and column.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
Taking this simple sql for example: </p><div class="Element13"><div class="Element12"><pre class="Element12">select table1.f1, t1.f2 as t1f2
from table1 t1
where t1.f3 = 1</pre></div></div>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
Token 'table1' in from clause is a declarative table token, while token 'table1' in select list is a reference table token.&nbsp;</p>
<p class="Element10">
Token 't1' in from clause is a declarative table alias token, while token 't1' in select list and where clause is a reference table alias.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
How those database objects represented by source token? Here it is:&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st1: table1 source token in select list, st1.DBObjType is ttobjTable.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st2: f1 source token in select list,st2.DBObjType is ttObjField.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st3: t1 source token in select list,st3.DBObjType is ttObjTableAlias.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st4: f2 source token in select list,st4.DBObjType is ttObjField.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st5: table1 source token in from clause,st5.DBObjType is ttobjTable.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st6: t1 source token in from clause,st6.DBObjType is ttObjTableAlias.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st7: t1 source token in where clause,st7.DBObjType is ttObjTableAlias.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st8: f3 source token in where clause,st8.DBObjType is ttObjField.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st9: t1f2 source token in select list, st9.DBObjType is ttObjFieldAlias&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
Sometimes we may need something more, such as relationship between table1 token in select list and from clause;&nbsp;</p>
<p class="Element10">
Which table is f3 token in where clause belong to, and so on. This is where the following properties of <a href="TSourceToken.html" target="topic">TSourceToken</a> come from.&nbsp;</p>
<p class="Element10">
ParentToken, RelatedToken,ChildToken, RelatedTokens,RelatedSubquery.&nbsp;</p>
<p class="Element10">
Those properties have different meanings while source token stands for different database,and there are also depends&nbsp;</p>
<p class="Element10">
on whether source token is declarative or reference.&nbsp;</p>
<p class="Element10">
Let's continue of previous example to explain meanings of those properties.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st5 is a declarative table token:&nbsp;</p>
<p class="Element10">
st5.ParentToken, schema token if any, here it is null.&nbsp;</p>
<p class="Element10">
st5.RelatedToken, table alias of declarative table token if any, here it is st6.&nbsp;</p>
<p class="Element10">
st5.ChildToken, always be null.&nbsp;</p>
<p class="Element10">
st5.RelatedTokens, reference token of declarative table. here it is st1.&nbsp;</p>
<p class="Element10">
Or Fields attach to declarative table in-directly. (such as field in create table)&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st1 is a reference table token:&nbsp;</p>
<p class="Element10">
st1.ParentToken, schema token if any, here it is null.&nbsp;</p>
<p class="Element10">
st1.RelatedToken, declarative table token, here it is st5.&nbsp;</p>
<p class="Element10">
st1.ChildToken, field attach to this table token, here it is f1.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
Since both declarative table token and reference table token have the same DBObjType (ttobjTable),How to distinguish between those 2 table tokens?&nbsp;</p>
<p class="Element10">
The answer is RelatedToken property of this token, if it is null, then it must be declarative token, since reference token always has RelatedToken points to declarative table token. if it is not null, then we can check DBObjType property of RelatedToken, if RelatedToken.DBObjType is ttobjTable, then it's a reference token, if RelatedToken.DBObjType is ttObjTableAlias, then it's a declarative token.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st6 is a declarative table alias token:&nbsp;</p>
<p class="Element10">
st6.RelatedToken, declarative table token, here it is st5.&nbsp;</p>
<p class="Element10">
st6.RelatedTokens, reference tokens related to this table alias token, here it is st3,st7.&nbsp;</p>
<p class="Element10">
st6.RelatedSubquery, subquery if this table alias is link to a subquery in from clause, here it is null.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st3,st7 are reference table alias token:&nbsp;</p>
<p class="Element10">
st3.ChildToken, field attach to this table alias token, here it is f2(st4).&nbsp;</p>
<p class="Element10">
st7.ChildToken, field attach to this table alias token, here it is f3(st8).&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
Now, let's take a look at column.&nbsp;</p>
<p class="Element10">
st2 represents f1 in select list,it DBObjType is ttObjField. and it's ParentToken is st1 which is a reference table token that represents table1.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st4 represents f2 in select list, it DBObjType is ttObjField, and it's ParentToken is st3 which is a reference table alias token that&nbsp;</p>
<p class="Element10">
represent t1.&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
st9 represents t1f2 in select list, it DBObjType is ttObjFieldAlias&nbsp;</p>
<p class="Element10">
&nbsp;</p>
<p class="Element10">
There are other DBObjType that use you use besides ttObjTable,ttObjTableAlias,ttObjFieldAlias,&nbsp;</p>
<p class="Element10">
there are ttObjFunction,ttObjTrigger,ttobjSchema,ttobjDatabase,ttobjServer, and more database objects will be supported in new version.</p></div>
</div>
</div>
<!-- End Page Content -->

<!-- Begin Page Footer -->
<div class="Element93">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="100%">
<div class="Element3">
Gudu software http://www.sqlparser.com</div>
</td></tr><tr><td width="100%">
<div class="Element97">
Send <a href="#" onclick="sendFeedback('info@sqlparser.com', 'Documentation Feedback', 'Project: General SQL Parser Help file%0ATopic ID: Source token and database object%0ATitle: Source token and database object');">comments</a> about this topic.</div>
</td></tr></table></div>

<!-- End Page Footer -->
</div>
</div>

<!-- End Client Area -->
</body></html>